<script type="text/javascript">
    var uploader = null;
    $(function () {
        uploader = new plupload.Uploader({
            runtimes: 'html5,flash,silverlight,html4',
            browse_button: '{{elem_id}}_pickfiles',
            container: document.getElementById('{{elem_id}}_container'),
            url: '{{reverse_url("upload", elem_id)}}?_xsrf='+Cookies.get("_xsrf"),
            flash_swf_url: '{{ static_url("js/plupload/Moxie.swf") }}',
            silverlight_xap_url: '{{ static_url("js/plupload/Moxie.xap") }}',
            chunk_size : 0,

            filters: {
                max_file_size: '{{max_file_size}}mb',
                mime_types: [
                    {title: "Upload files", extensions: "{{file_extensions}}"}
                ]
            },

            init: {
                PostInit: function () {
                    document.getElementById('{{elem_id}}_filelist').innerHTML = '';
                    document.getElementById('{{elem_id}}_uploadfiles').onclick = function () {
                        uploader.start();
                        return false;
                    };
                },

                FilesAdded: function (up, files) {
                    plupload.each(files, function (file) {
                        document.getElementById('{{elem_id}}_filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
                    });
                },

                UploadProgress: function (up, file) {
                    document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
                },

                FileUploaded: function(up, file, info) {
                    var info_ = $.parseJSON(info.response);
                    var params = {
                        //"loaded": file.loaded,
                        "name": file.name,
                        //"origSize": file.origSize,
                        "size": file.size,
                        "type": file.type,
                        "tt_id": "{{term_taxonomy_id}}",
                        "file_name": info_["file_name"]
                    };

                    $.get('{{reverse_url("upload", elem_id)}}', bo_xsrf(params), function (rsp) {
                        {% if uploaded_javascript_callback%}
                        var func = eval("{{uploaded_javascript_callback}}");
                        func(rsp, file);
                        {% end %}
                    });
                },

                Error: function (up, err) {
                    console.log(err);
                }
            }
        });
        uploader.init();
    });
</script>